<?php


namespace app\admin\controller;


use app\admin\model\ArticleCateModel;
use app\admin\model\ArticleModel;
use app\admin\model\ExampleCateModel;
use app\admin\model\ExampleIndustryModel;
use app\admin\model\ExampleModel;
use think\Db;

class Example extends Base
{
    /**
     * [index 文章列表]
     * @return mixed|\think\response\Json
     */

    public function index()
    {

        if (request()->isAjax()) {

            extract(input());

            $map = [];

            if (isset($key) && $key != "") {

                $map['r.title'] = ['like', "%" . $key . "%"];

            }

            if (isset($start) && $start != "" && isset($end) && $end == "") {

                $map['r.create_time'] = ['>= time', $start];

            }

            if (isset($end) && $end != "" && isset($start) && $start == "") {

                $map['r.create_time'] = ['<= time', $end];

            }

            if (isset($start) && $start != "" && isset($end) && $end != "") {

                $map['r.create_time'] = ['between time', [$start, $end]];

            }

            $field = input('field');//字段

            $order = input('order');//排序方式

            if ($field && $order) {

                $od = "r." . $field . " " . $order;

            } else {

                $od = "r.create_time desc";

            }

            $Nowpage = input('get.page') ? input('get.page') : 1;

            $limits = input("limit") ? input("limit") : 10;

            $count = Db::name('example')->alias('r')->where($map)->count();//计算总页面

            $example = new ExampleModel();

            $lists = $example->getExampleByWhere($map, $Nowpage, $limits, $od);

            for ($i = 0; $i < count($lists); $i++) {

                $photo = explode(',', $lists[$i]['photo']);

                $lists[$i]['photo'] = array_values($photo);

            }

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);

        }

        $codepay_config['host'] = (isHTTPS() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'];

        $servername = $codepay_config['host'] . '/detail/';

        return $this->fetch("example/index", ['servername' => $servername]);

    }


    /**
     * [getUserData 文章标签]
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */

    public function getUserData()
    {

        if (request()->isGet()) {

            extract(input());

            $map = [];

            if (isset($keyword) && $keyword != "") {

                $map['name'] = ['like', "%" . $keyword . "%"];

            }

            $Nowpage = input('get.page') ? input('get.page') : 1;

            $limits = input("limit") ? input("limit") : 10;

            $count = Db::name('label')->where($map)->count();//计算总页面

            $lists = Db::name('label')
                ->where($map)
                ->order('sort desc')
                ->page($Nowpage, $limits)
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);

        }

        if (request()->isPost()) {

            $data = Db::name('label')
                ->where('id', 'in', input('id'))
                ->select();

            return json(['code' => 200, 'data' => $data]);

        }

    }


    /**
     * select框动态搜索
     */

    public function get_formselectlist()
    {

        $value = input('keyword', '');

        $value ? $where = ['name' => ['like', '%' . $value . '%']] : $where = [];

        $list = db('label')
            ->where($where)
            ->order('sort', 'desc')
            ->field("id as value,name,'' as selected,'' as disabled")
            ->limit(0, 5)
            ->select();

        $list ? $list = $list : $list = [['name' => $value, 'value' => time(), 'selected' => '', 'disabled' => '']];

        return json(['code' => 0, 'msg' => 'success', 'data' => $list]);

    }

    /**
     * 获取分类下面的行业
     */
    public function get_industry()
    {
        if (request()->isPost()) {
            $param = input();
            $cate_id = $param['cate_id'];
            $list = db('example_industry')->where('cate_id', '=', $cate_id)->select();
            return json($list);

        }
    }

    /**
     * [add_article 添加文章]
     * @return mixed|\think\response\Json
     */

    public function add_example()

    {

        if (request()->isPost()) {

            extract(input());

            $param = input('post.');

            if (!isset($status)) {

                $param['status'] = 1;

            }

            $param['photo'] = trim($param['photo'], ',');

            $article = new ExampleModel();

            $flag = $article->insertExample($param);

            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

        }

        $cate = new ExampleCateModel();

        $industry = new ExampleIndustryModel();

        return $this->fetch('example/add_example', ['cate' => $cate->getCate(), 'industry' => $industry->getCate(), 'configState' => config('qiniu_allow_state')]);

    }


    /**
     * [edit_article 编辑文章]
     * @return mixed|\think\response\Json
     */

    public function edit_example()

    {

        $example = new ExampleModel();

        if (request()->isPost()) {

            $param = input('post.');

            $param['photo'] = trim($param['photo'], ',');

            $param['update_time'] = time();

            $flag = $example->updateArticle($param);

            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

        }

        $id = input('param.id');



        $data = $example->getOneArticle($id);

        if (!empty($data['photo'])) {

            $data['photo'] = trim($data['photo'], ',');

        } else {

            $data['photo'] = '';

        }
        $cate = new ExampleCateModel();

        $industry = new ExampleIndustryModel();
        //获取案例的cate_id
        $cate_id=$example->find($id);
        $cate_id=$cate_id['cate_id'];
        //通过cate_id获取industry
        $industryRes=$industry->where('cate_id','eq',$cate_id)->select();
        $this->assign([
            'cate'=>$cate->getCate(),
            'industry'=>$industryRes,
            'example'=>$data,
            'configState'=>config('qiniu_allow_state'),
        ]);

        return $this->fetch('example/edit_example');

    }


    /**
     * [del_article 删除文章]
     * @return mixed|\think\response\Json
     */

    public function del_example()

    {

        $id = input('param.id');

        $cate = new ExampleModel();

        $flag = $cate->delExample($id);

        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

    }


    /**
     * [article_state 文章状态]
     * @return \think\response\Json
     */

    public function article_state()

    {

        extract(input());

        $cate = new ArticleModel();

        $flag = $cate->articleState($id, $num);

        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

    }
    //*********************************************行业管理*********************************************//

    /**
     * [index_industry]
     * @return mixed|\think\response\Json
     */

    public function index_industry()
    {
        if (request()->isAjax()) {

            extract(input());


            $map = [];

            if (isset($key) && $key != "") {

                $map['name'] = ['like', "%" . $key . "%"];

            }

            if (isset($start) && $start != "" && isset($end) && $end == "") {

                $map['create_time'] = ['>= time', $start];

            }

            if (isset($end) && $end != "" && isset($start) && $start == "") {

                $map['create_time'] = ['<= time', $end];

            }

            if (isset($start) && $start != "" && isset($end) && $end != "") {

                $map['create_time'] = ['between time', [$start, $end]];

            }

            $field = input('field');//字段

            $order = input('order');//排序方式

            if ($field && $order) {

                $od = $field . " " . $order;

            } else {

                $od = "create_time desc";

            }

            $cate = new ExampleIndustryModel();

            $nowpage = input('get.page') ? input('get.page') : 1;

            $limits = input("limit") ? input("limit") : 10;


            $count = $cate->getAllCount($map);//计算总页面

            $lists = $cate->getAllCate($map, $nowpage, $limits, $od);

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);

        }

        return $this->fetch("example/index_industry");
    }

    /**
     * [add_cate 添加分类]
     * @return mixed|\think\response\Json
     */

    public function add_industry()

    {
        if (request()->isPost()) {

            extract(input());

            $param = input('post.');

            if (!isset($status)) {

                $param['status'] = 2;

            }

            $cate = new ExampleIndustryModel();
            //获取案例分类

            $flag = $cate->insertCate($param);

            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

        }

        $cate = new ExampleCateModel();

        $this->assign([
            'cate' => $cate->getCate()
        ]);

        return $this->fetch();

    }

    /**
     * [edit_cate 编辑分类]
     * @return mixed|\think\response\Json
     */

    public function edit_industry()

    {


        $industry = new ExampleIndustryModel();

        $cate = new ExampleCateModel();

        if (request()->isPost()) {

            $param = input('post.');

            $flag = $industry->editCate($param);

            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

        }

        $id = input('param.id');


        $this->assign([
            'industry' => $industry->getOneCate($id),
            'cate' => $cate->getCate(),
        ]);

        return $this->fetch();

    }

    /**
     * [del_cate 删除分类]
     * @return \think\response\Json
     */

    public function del_industry()

    {

        $id = input('param.id');

        $cate = new ExampleIndustryModel();

        $flag = $cate->delCate($id);

        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

    }

    /**
     * [cate_state 分类状态]
     * @return \think\response\Json
     */

    public function industry_status()

    {

        extract(input());

        $cate = new ExampleIndustryModel();

        $flag = $cate->cateState($id, $num);

        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

    }

    //*********************************************分类管理*********************************************//

    /**
     * [index_cate 分类列表]
     * @return mixed|\think\response\Json
     */

    public function index_cate()
    {

        if (request()->isAjax()) {

            extract(input());

            $map = [];

            if (isset($key) && $key != "") {

                $map['name'] = ['like', "%" . $key . "%"];

            }

            if (isset($start) && $start != "" && isset($end) && $end == "") {

                $map['create_time'] = ['>= time', $start];

            }

            if (isset($end) && $end != "" && isset($start) && $start == "") {

                $map['create_time'] = ['<= time', $end];

            }

            if (isset($start) && $start != "" && isset($end) && $end != "") {

                $map['create_time'] = ['between time', [$start, $end]];

            }

            $field = input('field');//字段

            $order = input('order');//排序方式

            if ($field && $order) {

                $od = $field . " " . $order;

            } else {

                $od = "create_time desc";

            }

            $cate = new ExampleCateModel();

            $nowpage = input('get.page') ? input('get.page') : 1;

            $limits = input("limit") ? input("limit") : 10;

            $count = $cate->getAllCount($map);//计算总页面

            $lists = $cate->getAllCate($map, $nowpage, $limits, $od);

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);

        }

        return $this->fetch("example/index_cate");

    }

    /**
     * [add_cate 添加分类]
     * @return mixed|\think\response\Json
     */

    public function add_cate()

    {

        if (request()->isPost()) {

            extract(input());

            $param = input('post.');

            if (!isset($status)) {

                $param['status'] = 2;

            }

            $cate = new ExampleCateModel();

            $flag = $cate->insertCate($param);

            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

        }


        return $this->fetch();

    }

    /**
     * [edit_cate 编辑分类]
     * @return mixed|\think\response\Json
     */

    public function edit_cate()

    {

        $cate = new ExampleCateModel();

        if (request()->isPost()) {

            $param = input('post.');

            $flag = $cate->editCate($param);

            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

        }

        $id = input('param.id');

        $this->assign('cate', $cate->getOneCate($id));


        return $this->fetch();

    }

    /**
     * [del_cate 删除分类]
     * @return \think\response\Json
     */

    public function del_cate()

    {

        $id = input('param.id');

        $cate = new ExampleCateModel();

        $flag = $cate->delCate($id);

        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

    }

    /**
     * [cate_state 分类状态]
     * @return \think\response\Json
     */

    public function cate_state()

    {

        extract(input());

        $cate = new ExampleCateModel();

        $flag = $cate->cateState($id, $num);

        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);

    }



    /**
     * [batchDelCate 批量删除文章分类]
     * @return \think\response\Json
     */

    //暂时不能用
    public function batchDelCate()
    {

        extract(input());

        if (empty($ids)) {

            return json(['code' => 100, 'msg' => '请选择要删除的记录！']);

        }

        $cate = new ExampleCateModel();

        $flag = $cate->batchDelCate($ids);

        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);

    }


    /**
     * [usingCate 批量启用分类]
     * @return \think\response\Json
     */

    public function usingCate()
    {

        extract(input());

        $list = [];

        if ($ids) {

            $ids = explode(',', $ids);

            for ($i = 0; $i < count($ids); $i++) {

                $param = [

                    'id' => $ids[$i],

                    'status' => 1

                ];

                $list[] = $param;

            }

        }

        $cate = new ExampleCateModel();

        $flag = $cate->usingCate($list);

        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);

    }


    /**
     * [forbiddenCate 批量禁用分类]
     * @return \think\response\Json
     */

    public function forbiddenCate()
    {

        extract(input());

        $list = [];

        if ($ids) {

            $ids = explode(',', $ids);

            for ($i = 0; $i < count($ids); $i++) {

                $param = [

                    'id' => $ids[$i],

                    'status' => 2

                ];

                $list[] = $param;

            }

        }

        $cate = new ExampleCateModel();

        $flag = $cate->forbiddenCate($list);

        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);

    }
}